package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.OrderInfoBean;
import com.project.bean.UserBean;
import com.project.mapper.IOrderInfoMapper;
import com.project.mapper.IOrderMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImp implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IOrderInfoMapper orderInfoMapper;


    @Override
    public List<OrderBean> findByItem(String productType, LocalDate date, String userType) {
        List<OrderBean> orderIdBeans = orderMapper.findByItem(productType, date, userType);
        return orderIdBeans;
    }

    @Override
    public IPage<UserBean> findByUserPrice(int pageNO) {
        Page<UserBean> page = new Page<>(pageNO,3);
        return orderMapper.findByUserPrice(page);
    }
    @Override
    public IPage<OrderBean> cutOrderByItem(int page, String type, LocalDate startDate, LocalDate endDate) {
        if (type !=null && type !=""){
            return  orderMapper.cutOrderByItemS(new Page<>(page,3),type,startDate,endDate);
        }
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        if (startDate !=null ){
            qw.ge("date",startDate);
        }
        if (endDate !=null ){
            qw.le("date",endDate);
        }


        return orderMapper.selectPage(new Page<>(page,3),qw);
    }

    @Override
    public OrderBean findOrderByOrderId(String orderId) {
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.eq("orderNum",orderId);
        return orderMapper.selectOne(qw);
    }

    @Override
    public void updateStatus(String orderId) {
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.eq("orderNum",orderId);
        OrderBean orderBean=orderMapper.selectOne(qw);
        orderBean.setStatus("已退货");
        orderMapper.updateById(orderBean);
    }

    @Override
    public List<OrderInfoBean> findByOrderId(String orderId) {
        QueryWrapper<OrderInfoBean> qw=new QueryWrapper<>();
        qw.eq("fk_order",orderId);
        return orderInfoMapper.selectList(qw);
    }

    @Override
    public String updateStatus(String orderNum, String status) {
        if (orderNum ==null || orderNum ==""){
            return "订单编号为空";
        }
        if (status ==null || status ==""){
            return "状态为空";
        }
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.eq("orderNum",orderNum);
        OrderBean orderObj=orderMapper.selectOne(qw);
        if (orderObj==null){
            return "订单编号错误";
        }
        orderObj.setStatus(status);
        orderMapper.updateById(orderObj);
        return "ok";
    }

    @Override
    public String addOrder(OrderBean orderBean) {
        if (orderBean==null){//判断订单对象是否为空
            return "订单对象为空";
        }
        if (orderBean.getOrderInfoList()==null || orderBean.getOrderInfoList().size()==0 ){//判断订单明细集合是否为空
            return "订单明细为空";
        }
        //添加订单
        orderMapper.insert(orderBean);
        //循环添加订单明细
        orderBean.getOrderInfoList().forEach(orderInfoBean -> {
            orderInfoBean.setOrderId(orderBean.getOrderNum());
            orderInfoMapper.insert(orderInfoBean);
        });
        return "ok";
    }
}
